package com.ltf.config;


import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//后端跨域
//访问的页面端口号和axios端口号访问不一致就形成了跨域
@Component //会自动放进IOC容器
@Order(Ordered.HIGHEST_PRECEDENCE) //把过滤器前置，优先级最高，这样放行跨域访问
public class MyCorsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain chain) throws IOException, ServletException {
       HttpServletRequest request=(HttpServletRequest) req;
        System.out.println("跨域请求路径："+request.getServletPath());
        HttpServletResponse response=(HttpServletResponse) resp;
        response.setHeader("Access-Control-Allow-Origin","*");//
        response.setHeader("Access-Control-Allow-Methods","*");
        response.setHeader("Access-Control-Max-Age","3600");
        response.setHeader("Access-Control-Allow-Headers",
                "x-requested-with,authorization,Content-Type,Authorization,credential,X-XSRF-TOKEN");
        //chain.doFilter(req,resp);
        if ("OPTIONS".equalsIgnoreCase(request.getMethod())){
            response.setStatus(HttpServletResponse.SC_OK);
        }else {
            chain.doFilter(req,resp);
        }
    }

    @Override
    public void init(FilterConfig filterConfig){}

    @Override
    public void destroy(){}
}
